package org.hilo.boot.core.shiro;

import org.apache.shiro.session.Session;
import org.apache.shiro.session.SessionListener;
import org.apache.shiro.session.mgt.eis.SessionDAO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 过期调用SessionDAO销毁缓存中的session
 * 
 * @author zollty
 * @since 2017-07-02
 */
public class ShiroSessionListener implements SessionListener {
    private static final Logger logger = LoggerFactory.getLogger(ShiroSessionListener.class);

    private SessionDAO sessionDao;

    public ShiroSessionListener() {
    }

    public ShiroSessionListener(SessionDAO sessionDao) {
        this.sessionDao = sessionDao;
    }

    @Override
    public void onStart(Session session) {
        // 会话创建时触发
        logger.debug("session {} onStart", session.getId());
    }

    @Override
    public void onStop(Session session) {
        sessionDao.delete(session);
        logger.debug("session {} onStop", session.getId());
    }

    @Override
    public void onExpiration(Session session) {
        sessionDao.delete(session);
        logger.debug("session {} onExpiration", session.getId());
    }

    public void setSessionDao(SessionDAO sessionDao) {
        this.sessionDao = sessionDao;
    }

}
